package com.raymond.client.codec.encodeing;


import com.raymond.client.codec.BaseCodec;
import com.raymond.client.enums.MsgHead;
import com.raymond.client.msg.BaseMessage;
import io.netty.buffer.ByteBuf;


/**
 * 编码器
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-07-14 14:55
 */
public abstract class BaseEncoder extends BaseCodec {

    public void encode(BaseMessage msg, ByteBuf out) throws Exception {
        byte[] body = encode0(msg);
        out.writeInt(body.length + MsgHead.getHeadLength());
        out.writeInt(msg.getCommandId());
        out.writeInt(msg.getSequenceId());
        out.writeBytes(body);
    }

    /**
     * 消息编码
     * @param msg 响应提
     * @return 请求报文
     * @throws Exception 异常
     */
    public abstract byte[] encode0(BaseMessage msg) throws Exception;
}
